from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional
from decimal import Decimal


class ProductBase(BaseModel):
    """商品基础模型"""
    name: str = Field(..., min_length=1, max_length=200, description="商品名称")
    description: Optional[str] = Field(None, description="商品描述")
    price: Decimal = Field(..., gt=0, description="商品价格")
    original_price: Optional[Decimal] = Field(None, gt=0, description="原价")
    stock: int = Field(default=0, ge=0, description="库存数量")
    category_id: int = Field(..., description="商品分类ID")
    image_url: Optional[str] = Field(None, max_length=500, description="商品图片URL")
    sku: Optional[str] = Field(None, min_length=1, max_length=100, description="商品SKU")
    is_active: bool = Field(default=True, description="是否上架")
    is_featured: bool = Field(default=False, description="是否推荐商品")


class ProductCreate(BaseModel):
    """创建商品模型"""
    name: str = Field(..., min_length=1, max_length=200, description="商品名称")
    description: Optional[str] = Field(None, description="商品描述")
    price: Decimal = Field(..., gt=0, description="商品价格")
    original_price: Optional[Decimal] = Field(None, gt=0, description="原价")
    stock: int = Field(default=0, ge=0, description="库存数量")
    category_id: int = Field(..., description="商品分类ID")
    image_url: Optional[str] = Field(None, max_length=500, description="商品图片URL")
    is_active: bool = Field(default=True, description="是否上架")
    is_featured: bool = Field(default=False, description="是否推荐商品")


class ProductUpdate(BaseModel):
    """更新商品模型"""
    name: Optional[str] = Field(None, min_length=1, max_length=200, description="商品名称")
    description: Optional[str] = Field(None, description="商品描述")
    price: Optional[Decimal] = Field(None, gt=0, description="商品价格")
    original_price: Optional[Decimal] = Field(None, gt=0, description="原价")
    stock: Optional[int] = Field(None, ge=0, description="库存数量")
    category_id: Optional[int] = Field(None, description="商品分类ID")
    image_url: Optional[str] = Field(None, max_length=500, description="商品图片URL")
    sku: Optional[str] = Field(None, min_length=1, max_length=100, description="商品SKU")
    is_active: Optional[bool] = Field(None, description="是否上架")
    is_featured: Optional[bool] = Field(None, description="是否推荐商品")


class ProductResponse(ProductBase):
    """商品响应模型"""
    id: int = Field(..., description="商品ID")
    sales: int = Field(..., description="销量")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

    class Config:
        from_attributes = True


class ProductListResponse(BaseModel):
    """商品列表响应模型"""
    products: list[ProductResponse] = Field(..., description="商品列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页大小")
    pages: int = Field(..., description="总页数")


class ProductQuery(BaseModel):
    """商品查询参数"""
    name: Optional[str] = Field(None, description="商品名称(模糊搜索)")
    category_id: Optional[int] = Field(None, description="分类ID")
    min_price: Optional[Decimal] = Field(None, ge=0, description="最低价格")
    max_price: Optional[Decimal] = Field(None, ge=0, description="最高价格")
    is_active: Optional[bool] = Field(None, description="是否上架")
    is_featured: Optional[bool] = Field(None, description="是否推荐商品")
    page: int = Field(default=1, ge=1, description="页码")
    size: int = Field(default=10, ge=1, le=100, description="每页大小")